import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.JTextArea;
import javax.swing.SwingWorker;

import org.xml.sax.InputSource;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.feed.synd.SyndEntry;


class NewsReaderWithRSS extends SwingWorker<String, String> {
    
    private String feedURL;
    private JTextArea text;

    public NewsReaderWithRSS (String feedURL, JTextArea text){
        this.feedURL = feedURL;
        this.text = text;
    }
    
    @Override public String doInBackground(){
        
        StringBuilder result = new StringBuilder();
        SyndFeedInput feedInput;
        feedInput = new SyndFeedInput();

        SyndFeed feed = null;

        try {
               feed = feedInput.build(new InputSource(feedURL));
            }     
        catch (Exception e) {
               System.err.println("Unable to parse feed from: " + feedURL);
               e.printStackTrace();
        }
         
        List<SyndEntry> entryList = feed.getEntries();
        
        for (int i = 0; i < entryList.size(); i++) {            
            result.append("Title: " + (entryList.get(i)).getTitle() + "\n" +
                    "Link: " + (entryList.get(i)).getLink() +
                    "\n" + 
                    "\n");
        }
        
        return result.toString();
       
    }
    
    @Override protected void done(){
        
        try {
            text.setText(get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        
    }
    
    @Override protected void process(List<String> progress){
        System.out.println(progress);
    }
    
}